package com.sourcethought.xawz;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.mobclick.android.MobclickAgent;
import com.sourcethought.xawz.view.SegmentedRadioGroup;

public class XawzActivity extends Activity {
	/** Called when the activity is first created. */
	private Button searchButton;
	private SegmentedRadioGroup carTypeSegmentedRadioGroup;
	private SegmentedRadioGroup searchTypeSegmentedRadioGroup;
	private String carType;
	private String searchType;
	private EditText carNumberEditText;
	private EditText keywordEditText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 统计分析工具
		MobclickAgent.setUpdateOnlyWifi(false);
		MobclickAgent.update(this);
		MobclickAgent.onError(this);

		carType = "02";
		searchType = "SYR";

		carTypeSegmentedRadioGroup = (SegmentedRadioGroup) findViewById(R.id.carTypeSegment);
		carTypeSegmentedRadioGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					public void onCheckedChanged(RadioGroup group, int checkedId) {
						carTypeSegmentedRadioGroupOnCheckedChanged(group,
								checkedId);
					}
				});

		searchTypeSegmentedRadioGroup = (SegmentedRadioGroup) findViewById(R.id.searchTypeSegment);
		searchTypeSegmentedRadioGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					public void onCheckedChanged(RadioGroup group, int checkedId) {
						searchTypeSegmentedRadioGroupOnCheckedChanged(group,
								checkedId);

					}

				});

		carNumberEditText = (EditText) findViewById(R.id.carNumberEditText);

		keywordEditText = (EditText) findViewById(R.id.keywordEditText);

		searchButton = (Button) findViewById(R.id.btn_search);

		searchButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				doSearch();
			}
		});

		restoreUserInfo();
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		// 存储用户操作信息
		saveUserInfo();
	}

	protected void saveUserInfo() {
		SharedPreferences pref = getPreferences(MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.putString("carType", carType);
		editor.putString("searchType", searchType);
		editor.putString("carNumber", carNumberEditText.getText().toString()
				.toUpperCase());
		editor.putString("keyword", keywordEditText.getText().toString());
		editor.commit();
	}

	protected void restoreUserInfo() {
		SharedPreferences pref = getPreferences(MODE_PRIVATE);
		carType = pref.getString("carType", "02");
		searchType = pref.getString("searchType", "SYR");
		String carNumber = pref.getString("carNumber", "");
		String keyword = pref.getString("keyword", "");

		if (carType.equals("02")) {
			carTypeSegmentedRadioGroup.check(R.id.smallCarTypeRadioButton);
		} else if (carType.equals("01")) {
			carTypeSegmentedRadioGroup.check(R.id.bigCarTypeRadioButton);
		}

		if (searchType.equals("SYR")) {
			searchTypeSegmentedRadioGroup.check(R.id.nameRadioButton);
		} else if (searchType.equals("SFZMHM")) {
			searchTypeSegmentedRadioGroup.check(R.id.idCardRadioButton);
		} else if (searchType.equals("FDJH")) {
			searchTypeSegmentedRadioGroup.check(R.id.engineNumberRadioButton);
		} else if (searchType.equals("CLSBDH")) {
			searchTypeSegmentedRadioGroup.check(R.id.frameNumberRadioButton);
		}

		keywordEditText.setText(keyword);
		carNumberEditText.setText(carNumber);
	}

	protected void carTypeSegmentedRadioGroupOnCheckedChanged(RadioGroup group,
			int checkedId) {
		if (checkedId == R.id.smallCarTypeRadioButton) {
			carType = "02";
		} else if (checkedId == R.id.bigCarTypeRadioButton) {
			carType = "01";
		}
	}

	protected void searchTypeSegmentedRadioGroupOnCheckedChanged(
			RadioGroup group, int checkedId) {
		String hint = null;

		if (checkedId == R.id.nameRadioButton) {
			hint = "请输入车主姓名";
			searchType = "SYR";
		} else if (checkedId == R.id.idCardRadioButton) {
			hint = "请输入身份证号";
			searchType = "SFZMHM";
		} else if (checkedId == R.id.engineNumberRadioButton) {
			hint = "请输入引擎号";
			searchType = "FDJH";
		} else if (checkedId == R.id.frameNumberRadioButton) {
			hint = "请输入车架号";
			searchType = "CLSBDH";
		}

		keywordEditText.setText(null);
		keywordEditText.setHint(hint);
	}

	private void doSearch() {

		if (keywordEditText.getText().toString().equals("")
				|| keywordEditText.getText() == null) {
			String msg = null;
			int checkedId = searchTypeSegmentedRadioGroup
					.getCheckedRadioButtonId();

			if (checkedId == R.id.nameRadioButton) {
				msg = "请输入车主姓名";
			} else if (checkedId == R.id.idCardRadioButton) {
				msg = "请输入身份证号";
			} else if (checkedId == R.id.engineNumberRadioButton) {
				msg = "请输入引擎号";
			} else if (checkedId == R.id.frameNumberRadioButton) {
				msg = "请输入车架号";
			}

			Toast.makeText(XawzActivity.this, msg, Toast.LENGTH_SHORT).show();
			return;
		}

		if (carNumberEditText.getText().toString().equals("")
				|| carNumberEditText.getText() == null) {

			Toast.makeText(XawzActivity.this, "请收入车牌号", Toast.LENGTH_SHORT)
					.show();
			return;
		}

		Intent intent = new Intent();
		intent.setClass(XawzActivity.this, SearchActivity.class);
		intent.putExtra("car_type", carType);
		intent.putExtra("search_type", searchType);
		intent.putExtra("car_nbr", carNumberEditText.getText().toString()
				.toUpperCase());
		intent.putExtra("note_text", keywordEditText.getText().toString());
		startActivity(intent);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			final AlertDialog isExit = new AlertDialog.Builder(this).create();
			isExit.setTitle("提示");
			isExit.setMessage("确定要退出程序吗");
			DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					switch (which) {
					case AlertDialog.BUTTON1:
						finish();
						break;
					case AlertDialog.BUTTON2:
						isExit.cancel();
						break;
					default:
						break;
					}
				}
			};
			isExit.setButton("确定", listener);
			isExit.setButton2("取消", listener);
			isExit.show();
			return Boolean.FALSE;
		}
		return Boolean.FALSE;
	}

}